package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.rfid.project.exception.CannotDelException;
import com.mysema.query.BooleanBuilder;

import java.util.List;

@Service
@Transactional
public class DepartmentService{

	@Autowired
	private DepartmentDao departmentDao;

	@Autowired
	private PeopleDao peopleDao;
	
	private QPeople $_ = QPeople.people;
	
	private QDepartment $ = QDepartment.department;
	
	@Autowired
	public void setDepartmentDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}
	
	@Autowired
	public void setPeopleDao(PeopleDao peopleDao) {
		this.peopleDao = peopleDao;
	}
	
	public List<Department> findAll(){
		return ListUtil.toList(departmentDao.findAll());
	}
				
	public List<Department> findAll(
	String name,
	String remark,
	String orderByAttribute	){
	BooleanBuilder condition = new BooleanBuilder();
	
	if(StringUtil.isNotEmpty(name)){
		condition.and($.name.contains(name));
	}
	
	
	if(StringUtil.isNotEmpty(remark)){
		condition.and($.remark.contains(remark));
	}
	
	
	return ListUtil.toList(departmentDao.findAll(condition));
	}		

	public Page<Department> findAll(
			String name,
			String remark,
			int pageNumber, int pageSize,String sortAttribute,String orderDirection) {
			BooleanBuilder condition = new BooleanBuilder();
	
			if(StringUtil.isNotEmpty(name)){
				condition.and($.name.contains(name));
			}
			if(StringUtil.isNotEmpty(remark)){
				condition.and($.remark.contains(remark));
			}
	
			Sort sortObject = null;
			if(StringUtil.isEmpty(sortAttribute)){
				sortObject = new Sort(Direction.DESC, "id");
			}else{
				sortObject = new Sort(Direction.fromStringOrNull(orderDirection),sortAttribute);
			}
			
			PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, sortObject);
			return departmentDao.findAll(condition, pageRequest);
	}
	
	
	
	public Department getDepartment(Long id){
		return departmentDao.getOne(id);
	}
	
	public void deleteDepartment(Long id) throws CannotDelException{
		if (this.findPeopleByDept(id)) {
			throw new CannotDelException("部门已被人员引用，请先删除人员信息或修改人员的引用！");
		} else {
			departmentDao.delete(id);
		}
	}
	
	private boolean findPeopleByDept(Long deptId){
		BooleanBuilder condition = new BooleanBuilder();
		condition.and($_.department.id.eq(deptId));
		return this.peopleDao.count(condition) > 0;
	}
	
	

	public void saveDepartment(Department department) {
		if (null == department.getOrderNumber())
			department.setOrderNumber(this.getOrderNumber());
		departmentDao.save(department);
	}


	private int getOrderNumber(){
		BooleanBuilder condition = new BooleanBuilder();
		Sort sortObject = new Sort(Direction.DESC, "orderNumber");
		PageRequest pageRequest = new PageRequest(0, 1, sortObject);
		Page<Department> departments = this.departmentDao.findAll(condition,pageRequest);
		if(departments.getTotalElements() > 0){
			Department department = departments.getContent().get(0);
			return department.getOrderNumber()+1;
		}
		return 1;
	}

	public Page<Department> findAll(String deptname, Integer pageNumber,
			Integer pageSize) {
		Sort sortObject = new Sort(Direction.DESC, "orderNumber");
		BooleanBuilder condition = new BooleanBuilder();
		condition.and($.name.contains(deptname));
		PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize,sortObject);
		return this.departmentDao.findAll(condition, pageRequest);
	}
	
	public boolean changeOrderNumber(Long id, Long targetId){
		Department currentModule = this.departmentDao.findOne(id);
		Department targetModule = this.departmentDao.findOne(targetId);
		int orderNumber = currentModule.getOrderNumber();
		currentModule.setOrderNumber(targetModule.getOrderNumber());
		targetModule.setOrderNumber(orderNumber);
		
		this.departmentDao.save(currentModule);
		this.departmentDao.save(targetModule);
		return true;
	}
	

}
		
